package cn.com.ksplatform.modules.system.auth.cachemanager.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.ksplatform.common.logger.Log;
import cn.com.ksplatform.modules.system.auth.menumanager.model.PlatMenu;
import cn.com.ksplatform.modules.system.auth.menumanager.service.PlatMenuService;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * 菜单缓存
 * ClassName:MenuCacheService.java <br/>
 * Date: 2015年12月14日<br/>
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
@Component("system.modules.system.cache.MenuCacheService")
public class MenuCacheService {
	@Autowired
	@Qualifier("cacheManagerFactory")
	CacheManager cacheManager;
	
	Cache menuCache = null;
	
	public Cache getCache(){
		if(menuCache == null){
			menuCache = cacheManager.getCache("MenuCache");
		}
		return menuCache;
	}
	
	@Autowired
	PlatMenuService menuService;
	/**
	 * 初始化缓存信息
	 */
	public void init(){
		List<PlatMenu> menus = menuService.getAllMenus();
		for(PlatMenu menu : menus){
			Element em = new Element(menu.getMenuId(), menu);
			getCache().put(em);
		}
		Log.info("初始化菜单缓存数:"+menus.size());
	}
}
